import javafx.beans.binding.When;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;

public class Regist extends Component {
    JTextField textField = null;
    JPasswordField pwdField = null;
    JPasswordField repwdField = null;
    ClientReadAndPrint.LoginListen listener = null;
    JFrame registframe = new JFrame("注册");

    Connection conn = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;

    public Regist() {
        init();
    }

    void init() {
        JFrame jf = new JFrame("注册");
        jf.setBounds(500, 250, 350, 270);
        jf.setResizable(false);

        JPanel jp1 = new JPanel();
        JLabel headJLabel = new JLabel("注册界面");
        headJLabel.setFont(new Font(null, 0, 35));
        jp1.add(headJLabel);

        JPanel jp2 = new JPanel();
        JLabel nameJLabel = new JLabel("用户名：  ");
        textField = new JTextField(20);
        JLabel pwdJLabel = new JLabel("密码：      ");
        pwdField = new JPasswordField(20);
        JLabel repwdJLabel = new JLabel("确认密码：    ");
        repwdField = new JPasswordField(20);

        JButton loginButton = new JButton("返回");
        JButton registerButton = new JButton("注册");

        jp2.add(nameJLabel);
        jp2.add(textField);
        jp2.add(pwdJLabel);
        jp2.add(pwdField);
        jp2.add(repwdJLabel);
        jp2.add(repwdField);
        jp2.add(loginButton);
        jp2.add(registerButton);

        JPanel jp = new JPanel(new BorderLayout());
        jp.add(jp1, BorderLayout.NORTH);
        jp.add(jp2, BorderLayout.CENTER);

        // Add ActionListener to the Register button
        registerButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    register(); // Call the register method when the button is clicked
                } catch (ClassNotFoundException ex) {
                    throw new RuntimeException(ex);
                }
            }
        });

        loginButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Login login = new Login(); // Call the register method when the button is clicked
                jf.setVisible(false);
            }
        });

        jf.add(jp);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        jf.setVisible(true);
    }

    // Method to handle registration process
    void register() throws ClassNotFoundException {
        String username = textField.getText();
        String password = new String(pwdField.getPassword());
        String confirmPassword = new String(repwdField.getPassword());

        if(!password.equals(confirmPassword)){
            JOptionPane.showMessageDialog(this, "两次密码输入不一致，请重新输入！", "提示", JOptionPane.WARNING_MESSAGE);

        }else {
            try {
                // Register JDBC driver
                Class.forName("com.mysql.cj.jdbc.Driver");
                String url = "jdbc:mysql://localhost:3306/userinfo?serverTimezone=Asia/Shanghai";

                // Open a connection
                conn = DriverManager.getConnection(url, "root", "123456");

                // Prepare a SQL statement
                String sql = "insert into user_info(username,password) values(?,?)";
                PreparedStatement statement = conn.prepareStatement(sql);
                statement.setString(1, username);
                statement.setString(2, password);

                int rowsInserted = statement.executeUpdate();
                if (rowsInserted > 0) {
                    JOptionPane.showMessageDialog(registframe, "注册成功");

                }
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {
        Regist regist = new Regist();
    }
}